//
//  BTBFirstVC.swift
//  BlueToothBackground
//
//  Created by macvivi on 2021/11/19.
//

import UIKit
import CoreLocation


class BTBFirstVC: UIViewController, CLLocationManagerDelegate {
    
    lazy var locationManager: CLLocationManager =  {
        let locationManager = CLLocationManager()
        print(#function)
        locationManager.delegate = self
        return locationManager
    }()
    
    lazy var beaconRegion: CLBeaconRegion =  {
        let proximityUUID = UUID(uuidString:
                    "39ED98FF-2900-441A-802F-9C398FC199D2")
        let major : CLBeaconMajorValue = 100
        let minor : CLBeaconMinorValue = 1
        let beaconID = "com.example.myDeviceRegion"
        let beaconRegion = CLBeaconRegion(proximityUUID: proximityUUID!,
                                          major: major, minor: minor, identifier: beaconID)
//       let beaconRegion = CLBeaconRegion()
        print(#function)
        beaconRegion.notifyEntryStateOnDisplay = true
        beaconRegion.notifyOnEntry = true
        beaconRegion.notifyOnExit = true
        return beaconRegion
    }()
    
    func startRanging(){
        printXY(#function, obj: self, line: #line)
        locationManager.startRangingBeacons(in: beaconRegion)
        locationManager.startMonitoring(for: beaconRegion)
    }
    
    func stopRanging(){
        locationManager.stopRangingBeacons(in: beaconRegion)
    }
    
    
    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        printXY(beacons, obj: self, line: #line)
    }
    
    func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
        printXY(error, obj: self, line: #line)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        startRanging()
        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
